<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>

    <link rel="stylesheet" href="../../static/css/bootstrap.min.css">
    <script src="../../static/js/axios-0.18.0.js"></script>
    <script src="../../static/js/vue.js"></script>
    <script src="../../static/plugins/echarts/echarts.js"></script>
    <!--引入样式-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input @keyup.enter.native="findPage()" placeholder="昵称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <template>
                    <h4 class="el-icon-help">选择状态</h4>
                    <el-select placeholder="普通用户" v-model="findstatus" @change="findPage()">
                        <el-option key="1" label="普通用户" value="1"></el-option>
                        <el-option key="2" label="封禁用户" value="2"></el-option>
                    </el-select>
                </template>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column  align="center" label="头像">
                    <template slot-scope="scope">
                        <img :src="scope.row.uimage" style="width: 40px;height: 40px;"/>
                    </template>
                </el-table-column>
                <el-table-column prop="username" label="昵称" align="center"></el-table-column>
                <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
                <el-table-column prop="sex" label="性别" align="center"></el-table-column>
                <el-table-column prop="school" label="学校" align="center"></el-table-column>
                <el-table-column label="身份" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.userstatus === '1' ? '普通用户' : '违规用户'}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作"  align="center" width="200px">
                    <template slot-scope="scope">
                        <el-button-group>
                            <el-button v-if="scope.row.userstatus === '1'" type="danger" size="mini" @click="handleBan(scope.row)">封禁</el-button>
                            <el-button v-else type="primary" size="mini" @click="relieveBan(scope.row)">解除封禁</el-button>
                            <el-button size="mini" type="danger" @click="handleAdmin(scope.row)">设为管理员</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页-->
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            findstatus: '',
    /*{
        username:'韩政利',
            email:'1938121877@qq.com',
        sex:'男',
        school:'南阳理工学院',
        identity:'普通用户',
        userstatus: 1
    },
    {
        username:'周民全',
            email:'1938121877@qq.com',
        sex:'男',
        school:'南阳理工学院',
        identity:'普通用户',
        userstatus: 0
    }*/
            dataList: [],//当前页要展示的分页列表数据
            oneData:{},//行数据
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },

        methods: {
            //分页查询
            findPage() {
                console.log(this.findstatus);
                //发送ajax请求，发送分页请求参数（每页条数，查询条件）
                var page ={
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/user/findall?userstatus="+this.findstatus,page).then((res)=>{
                    //处理响应数据
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //页面更改时，再次请求数据
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //封禁用户
            handleBan(row){
                this.$confirm("确认封禁"+row.username+"吗?","提示",{type:'warning'}).then(()=>{
                    axios.get("/user/userban?userid="+row.userid).then((res)=>{
                        if(res.data.flag){
                            //封禁成功,重新请求页面
                            this.$message.success(res.data.message);
                            this.findPage();
                        }else{
                            this.$message.error(res.data.message);
                        }
                    }).catch(()=>{
                        //请求失败
                        this.$message.error("请求失败");
                    })
                }).catch(()=>{
                    this.$message.warning("已取消");
                })
            },
            quxiao(){
                this.$message.warning("已取消");
            },
            //解封用户
            relieveBan(row){
                this.$confirm("确认解封"+row.username+"吗？","提示",{type:"warning"}).then(()=>{
                    //axios请求，解封用户
                    axios.get("/user/relieve?userid="+row.userid).then((res)=>{
                        if(res.data.flag){
                            //成功，重新查询页面并其实
                            this.findPage();
                            this.$message.success(res.data.message);
                        }else{
                            //提示失败
                            this.$message.error(res.data.message);
                        }
                    })
                }).catch(()=>{
                    this.quxiao();
                })
            },
            //设置管理员
            handleAdmin(row){
                this.$confirm("确定要把用户"+row.username+"设置为管理员？","提示",{type:"warning"}).then(()=>{
                    axios.get("/user/setadmin?userid="+row.userid).then((res)=>{
                        if(res.data.flag){
                            this.$message.success(res.data.message);
                            this.findPage();
                        }else{
                            this.$message.error(res.data.message);
                        }
                    })
                }).catch(()=>{
                    this.quxiao();
                })
            }
        }
    })
</script>
</html>